package edu.ccut.saturn.studio.component.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


import edu.ccut.saturn.studio.component.common.CommonSet;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;

/**
 * 
 * @author Bai Yucheng  2007-12-3
 *
 */
public class EditParamDialog extends TitleAreaDialog {
	private Text descriptionText;
	private Combo scopeCombo;
	private Combo classTypeCombo;
	private Combo passTypeCombo;
	private Text valueKeyText;
	private Text nameText;
	protected Object result;
	private String name;
	private String valueKey;
	private String passingType;
	private String classType;
	private String scope;
	private String description;

	public static int i = 0;
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public EditParamDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginTop = 5;
		gridLayout.marginRight = 5;
		gridLayout.marginLeft = 5;
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		final Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setText(Messages.getString("EditParamDialog.name"));  //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		nameText.setLayoutData(gd_nameText);
		nameText.setText(name);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		
		final Label classtypeLabel = new Label(container, SWT.NONE);
		classtypeLabel.setText(Messages.getString("EditParamDialog.type")); //$NON-NLS-1$

		classTypeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_classTypeText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		classTypeCombo.setLayoutData(gd_classTypeText);
		classTypeCombo.setItems(CommonSet.classTypes);
		classTypeCombo.setText(classType);
		
		final Label passingtypeLabel = new Label(container, SWT.NONE);
		passingtypeLabel.setText(Messages.getString("EditParamDialog.inputoroutput"));  //$NON-NLS-1$

		passTypeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_passTypeText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		passTypeCombo.setLayoutData(gd_passTypeText);
		passTypeCombo.setItems(CommonSet.passingTypes);
		passTypeCombo.setText(passingType);

		final Label valuekeyLabel = new Label(container, SWT.NONE);
		valuekeyLabel.setText(Messages.getString("EditParamDialog.expression"));  //$NON-NLS-1$

		valueKeyText = new Text(container, SWT.BORDER);
		final GridData gd_valueKeyText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		valueKeyText.setLayoutData(gd_valueKeyText);
		valueKeyText.setText(valueKey);
		valueKeyText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
			
		final Label scopeLabel = new Label(container, SWT.NONE);
		scopeLabel.setText(Messages.getString("EditParamDialog.dataset")); //$NON-NLS-1$

		scopeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_scopeText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		scopeCombo.setLayoutData(gd_scopeText);
		scopeCombo.setItems(CommonSet.scopes);
		scopeCombo.setText(scope);

		final Label descriptionLabel = new Label(container, SWT.NONE);
		descriptionLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
		descriptionLabel.setText(Messages.getString("EditParamDialog.description")); //$NON-NLS-1$

		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER | SWT.H_SCROLL);
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_descriptionText.heightHint = 60;
		descriptionText.setLayoutData(gd_descriptionText);
		descriptionText.setText(description);
		
		setTitle(Messages.getString("EditParamDialog.editparameter")); //$NON-NLS-1$
		setMessage(Messages.getString("EditParamDialog.pleaseinputparameter"));  //$NON-NLS-1$
		return area;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages.getString("EditParamDialog.confirm"), true); //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages.getString("EditParamDialog.cancel"), false); //$NON-NLS-1$
		dialogChanged();
	}
	
	private void dialogChanged() {
		if (SaturnValidation.isEmpty(nameText.getText())) {
			setErrorMessage(Messages.getString("EditParamDialog.namecannotemputy")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		
		if (SaturnValidation.isEmpty(valueKeyText.getText())) {
			setErrorMessage(Messages.getString("EditParamDialog.expressioncannotempty")); //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		
		setErrorMessage(null);
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 375);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("EditParamDialog.editparameter")); //$NON-NLS-1$
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			i = 1;
			name = nameText.getText();
			valueKey = valueKeyText.getText();
			passingType = passTypeCombo.getText();
			classType = classTypeCombo.getText();
			scope = scopeCombo.getText();
			description = descriptionText.getText();
			close();
		}
		super.buttonPressed(buttonId);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValueKey() {
		return valueKey;
	}

	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	public String getPassingType() {
		return passingType;
	}

	public void setPassingType(String passingType) {
		this.passingType = passingType;
	}

	public String getClassType() {
		return classType;
	}

	public void setClassType(String classType) {
		this.classType = classType;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
